﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using Remind.IDAL;
using Remind.Model;
using Remind.DBUtility;

namespace Remind.AccessDAL
{
    public class Tel:ITel 
    {
        protected const string SQL_SELECT_TEL_BY_CID = "SELECT cid, phone, phone1, family, company, fax FROM tel where cid=@cid";
        protected const string SQL_INSERT_TEL = "INSERT INTO tel (cid, phone, phone1, family, company, fax) VALUES(@cid, @phone, @phone1, @family, @company, @fax)";

        protected const string PARM_CID = "@cid";

        #region ITel 成员

        public TelInfo GetTelByCid(int cid)
        {
            OleDbParameter parm = new OleDbParameter(PARM_CID, OleDbType.Integer);
            parm.Value = cid;
            using (OleDbDataReader rdr=AccessHelper.ExecuteReader(AccessHelper.ConnectionString,CommandType.Text,SQL_SELECT_TEL_BY_CID,parm))
            {
                TelInfo tel = null;
                if (rdr.Read())
                {
                    tel = new TelInfo();
                    if (!rdr.IsDBNull(0))
                        tel.Cid = rdr.GetInt32(0);
                    if (!rdr.IsDBNull(1))
                        tel.Phone = rdr.GetString(1);
                    if (!rdr.IsDBNull(2))
                        tel.Phone1 = rdr.GetString(2);
                    if (!rdr.IsDBNull(3))
                        tel.Family = rdr.GetString(3);
                    if (!rdr.IsDBNull(4))
                        tel.Company = rdr.GetString(4);
                    if (!rdr.IsDBNull(5))
                        tel.Fax = rdr.GetString(5);
                }
                return tel;
            }
        }

        public int UpdateTelByCid(TelInfo tel, Idu idu)
        {
            int resoult = 0;
            OleDbParameter[] parms = { 
                                         new OleDbParameter("@cid",OleDbType.Integer),
                                         new OleDbParameter("@phone",OleDbType.VarChar,16),
                                         new OleDbParameter("@phone1",OleDbType.VarChar,16),
                                         new OleDbParameter("@family",OleDbType.VarChar,16),
                                         new OleDbParameter("@company",OleDbType.VarChar,16),
                                         new OleDbParameter("@fax",OleDbType.VarChar,16)
                                     };
            parms[0].Value = tel.Cid;
            parms[1].Value = tel.Phone;
            parms[2].Value = tel.Phone1;
            parms[3].Value = tel.Family;
            parms[4].Value = tel.Company;
            parms[5].Value = tel.Fax;
            switch (idu)
            {
                case Idu.insert:
                    resoult = AccessHelper.ExecuteNonQuery(AccessHelper.ConnectionString, CommandType.Text, SQL_INSERT_TEL, parms);
                    break;
                case Idu.update:
                    break;
                case Idu.delete:
                    break;
                default:
                    break;
            }
            return resoult;
        }

        #endregion
    }
}
